package com.alstudio.utils.android.net.http.http2;

import java.util.LinkedList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.text.TextUtils;

public class HttpParameters {

	private List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();

	public HttpParameters() {

	}

	public void add(String name, String value) {
		if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) {
			params.add(new BasicNameValuePair(name, value));
		}
	}

	public boolean isEmpty() {
		return this.size() < 1;
	}

	public void add(String key, int value) {
		add(key, String.valueOf(value));
	}

	public void add(String key, long value) {
		add(key, String.valueOf(value));
	}

	public void addAll(HttpParameters parameters) {
		for (int i = 0; i < parameters.size(); i++) {
			add(parameters.getKey(i), parameters.getValue(i));
		}
	}

	public void remove(String key) {
		if (TextUtils.isEmpty(key))
			return;

		int size = params.size();
		for (int i = 0; i < size; ++i) {
			BasicNameValuePair item = params.get(i);
			if (item.getName().equals(key)) {
				params.remove(i);
				return;
			}
		}

		return;
	}

	public boolean isContainKey(String key) {
		if (TextUtils.isEmpty(key))
			return false;

		for (BasicNameValuePair item : params) {
			if (item.getName().equals(key))
				return true;
		}

		return false;
	}

	public String getKey(int index) {
		if (index >= 0 && index < params.size()) {
			return params.get(index).getName();
		}

		return "";
	}

	public String getValue(String key) {
		if (TextUtils.isEmpty(key))
			return null;

		int size = params.size();
		for (int i = 0; i < size; ++i) {
			BasicNameValuePair item = params.get(i);
			if (item.getName().equals(key))
				return item.getValue();
		}

		return null;
	}

	public String getValue(int index) {
		if (index >= 0 && index < params.size()) {
			return params.get(index).getValue();
		}

		return null;
	}

	public List<BasicNameValuePair> value() {
		return params;
	}

	public int size() {
		return params.size();
	}

	public void clear() {
		this.params.clear();
	}

}
